package com.tangding.service.dao.games.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Repository;

import com.tangding.cache.GamesConfigCache;
import com.tangding.common.exception.GException;
import com.tangding.common.logx.LogHelp;
import com.tangding.service.db.BaseMongoRepository;
import com.tangding.service.db.MongoDBOption;
import com.tangding.service.entity.games.config.GamesConfig;
import com.tangding.service.entity.games.config.Node;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年12月26日_上午10:48:51
 *
 */
@Repository
public class GamesConfigRepository extends BaseMongoRepository<GamesConfig, String>{

	
	private Map<String, GamesConfig> configs = new HashMap<>();
	private Map<String, List<Node>> normalDataConfigs = new HashMap<>();
	private Map<String, List<Node>> zhxDataConfigs = new HashMap<>();
	private Map<String, List<Node>> jackpotDataConfigs = new HashMap<>();
	
	@Override
	protected void afterInitClient() {
		option = new MongoDBOption("BACKEND_CONFIG", "GamesConfig");
		try {
			List<GamesConfig> list = findAll();
			list.forEach(config -> {
				configs.put(config.getGameName(), config);
				normalDataConfigs.put(config.getGameName(), config.getNormalData());
				if (config.getZhxData() != null) {
					zhxDataConfigs.put(config.getGameName(), config.getZhxData());
				}
				jackpotDataConfigs.put(config.getGameName(), config.getJackpotData());
				
				
				Map<String, Object> data = new HashMap<>();
				data.put("head", config.getNormalData());
				List<Node> nodes = config.getZhxData();
				if (nodes != null) {
					data.put("zhx", nodes);
				}
				data.put("bigRewards", config.getJackpotData());
				GamesConfigCache.configs.put(config.getGameName(), data);
			});
		} catch (GException e) {
			LogHelp.doLogAppErr(e);
			System.exit(-1);
			return;
		}
	}

	public List<Node> getZhxDataConfigs(String gameName) {
		return zhxDataConfigs.get(gameName);
	}
	
	public List<Node> getNormalDataConfigs(String gameName) {
		return normalDataConfigs.get(gameName);
	}
	
	public List<Node> getJackpotDataConfigs(String gameName) {
		return jackpotDataConfigs.get(gameName);
	}
	
	public Set<String> getGamesName() {
		return configs.keySet();
	}
}
